﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using AirCtrl.Wpf.Core;
using Microsoft.Extensions.DependencyInjection;

namespace AirCtrl.Wpf.Views
{
    /// <summary>
    /// CameraView.xaml 的交互逻辑
    /// </summary>
    public partial class CameraView : UserControl
    {
        private MainViewModel _mainVm;
        private CameraViewModel _vm;
        public CameraView()
        {
            InitializeComponent();

            _mainVm = App.Current.ServiceProvider.GetRequiredService<MainViewModel>();
        }


        private async void Image_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            var image = sender as Image;
            Point point = e.GetPosition(image);
            var x = Math.Round(point.X, 2);
            var y = Math.Round(point.Y, 2);

            var cameraTitle = image.Tag as string;
            var cameraVm = _mainVm.CameraVms.FirstOrDefault(vm => vm.Title == cameraTitle);

            if (cameraTitle == "长波热像仪")
            {
                var t = await CmdGroundClient.Instance.GetTemperature(x, y);
                cameraVm.Info = $"坐标：({x},{y})   温度：{t}℃";
            }
        }


        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            _vm = (CameraViewModel)DataContext;
            ButtonFullScreen.Visibility = Visibility.Hidden;
            _mainVm.CameraVms.Remove(_vm);
            var window = new Window();
            window.Title = _vm.Title;
            window.Closed += Window_Closed;
            window.SizeChanged += WindowOnSizeChanged;

            window.Content = this;
            window.Show();
        }

        private void WindowOnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.Height = e.NewSize.Height - 40;
            this.Width = e.NewSize.Width - 15;
        }


        private void Window_Closed(object? sender, EventArgs e)
        {
            ButtonFullScreen.Visibility = Visibility.Visible;
            _mainVm.CameraVms.Add(_vm);
        }
    }
}
